function ajax_post(form)
{
	var url = $(form).attr("action");
	var data = new Array();
	var VerificationThrough=true;
	
	$(form).find("input,select,textarea").each(function(i,n){
		var type = n.type;
		if(validate($(n))===false)
		{
			VerificationThrough=false;
		}
		switch(type)
		{
			case 'checkbox':
			case 'radio':
				if(n.checked)
				{
					data.push(n.name+'='+n.value);
				}
				break;
			default:
				var v = $(n).val();
				if(v)
				{
					v	= v.replace(/\+/g, '%2B');
					v	= v.replace(/\&/g, '%26');
				}
				data.push(n.name+'='+v);
				break;
		}
	});
	
	if(VerificationThrough)
	{
		$.ajax({
			url: url,
			type: 'POST',
			data: data.join('&'),
			dataType: 'json',
			success: function(ret) {
				if (ret.result) {
					if(typeof(ret.data.url)=='undefined' || ret.data.url=='')
					{
						afterlogin();
					}
					else
					{
						window.location.href= ret.data.url;
					}
				} else {
					showErrorMsg(form,ret.data.msg, '', '');
					return false;
				}
			},
			error:function(res){
				return false;
			}
		});
	}

	return false;
}
function showErrorMsg(form,msg)
{
	var form_error_tip = $(form).find(".form_error_tip");
	if(form_error_tip.length > 0)
	{
		form_error_tip.empty();
		form_error_tip.append(msg);
	}
	else
	{
		alert(msg);
	}
}
function validate(field)
{
    var rulesParsing = field.attr('class');
    var getRules = /validate\[(.*)\]/.exec(rulesParsing);
    if (getRules === null)
        return true;
    var str = getRules[1];
    var rules = str.split(/\[|,|\]/);
    
    for (var i = 0; i < rules.length; i++)
    {
    	switch (rules[i])
    	{
    		case 'funcCall':
    			return v__funcCall(field,rules,i);
    			break;
    		case 'required':
    			return v__required(field,rules,i);
    			break;
    	}
    }
}
function v__funcCall(field, rules, i) {	
    var functionName = rules[i + 1];
    var fn = window[functionName];
    if (typeof(fn) === 'function')
        return fn(field, rules, i);
}

function v__required(field,rules,i){
	msg = false;
	if(field.attr('type')=='radio')
	{
		if($("input[name='" + field.attr('name') + "']:checked").size()==0)
		{
			msg ='该项不能为空';
		}
	}
	if (field.val().trim()=='')
	{
		msg='该项不能为空';
	}
	return showError(field,msg);
}

function showError(field,msg)
{
	var errorSpan = field.next(".error_tip");
	if(msg)
	{
		errorSpan.empty();
		errorSpan.append(msg);
		return false;
	}
	else
	{
		errorSpan.empty();
		return true;
	}
}